import api from '../../api';
import { message } from 'antd';
import moment from 'moment';
export default {
  namespace: 'qualificationManagementPlanMaintenanceM',

  state: {
    assetTypeList: [], // 资产类别
    fundActualInvestList: [], // 资金实际投向
    planPropertyList: [], // 资产管理计划属性
    managementStyleList: [], // 管理方式
    buyConstructionList: [], //购买结构
    fundUseIndustryList: [],
    interestBasisList: [], //计息基础
    rateTypeList: [], //利率类型
    splitWayList: [], //拆分方式
    baseRate: [],
    interestMethodList: [], //计息方式
  },
  effects: {
    // 获取枚举接口
    *getDictionList({ payload, callback }, { call, put }) {
      const response = payload['dictData'];
      if (response) {
        // 资产类别
        let temAssetType = [];
        response.BAXXX_ZCLB &&
          response.BAXXX_ZCLB.length > 0 &&
          response.BAXXX_ZCLB.forEach((item) => {
            temAssetType.push({ title: item.value, value: item.code });
          });
        // 资金实际投向
        let tempFundActualInvest = [];
        response.fundActualInvest &&
          response.fundActualInvest.length > 0 &&
          response.fundActualInvest.forEach((item) => {
            tempFundActualInvest.push({ title: item.value, value: item.code });
          });
        // 资产管理计划属性
        let planProperty = [];
        response.BAXX_ZGJHSX &&
          response.BAXX_ZGJHSX.length > 0 &&
          response.BAXX_ZGJHSX.forEach((item) => {
            planProperty.push({ title: item.value, value: item.code });
          });
        // 管理方式
        let managementStyle = [];
        response.BAXXX_GLFS &&
          response.BAXXX_GLFS.length > 0 &&
          response.BAXXX_GLFS.forEach((item) => {
            managementStyle.push({ title: item.value, value: item.code });
          });
        // 购买结构
        let buyConstruction = [];
        response.buyConstruction &&
          response.buyConstruction.length > 0 &&
          response.buyConstruction.forEach((item) => {
            buyConstruction.push({ title: item.value, value: item.code });
          });
        //计息基础
        let interestBasis = [];
        response.ASSETS_INTEREST_BASIS &&
          response.ASSETS_INTEREST_BASIS.length > 0 &&
          response.ASSETS_INTEREST_BASIS.forEach((item) => {
            interestBasis.push({ title: item.value, value: item.code });
          });
        //利率类型
        let rateType = [];
        response.rateType &&
          response.rateType.length > 0 &&
          response.rateType.forEach((item) => {
            rateType.push({ title: item.value, value: item.code });
          });
        // 拆分方式
        let splitWayList = [];
        response.SPLIT_WAY_DICTIONARY &&
          response.SPLIT_WAY_DICTIONARY.length > 0 &&
          response.SPLIT_WAY_DICTIONARY.forEach((item) => {
            splitWayList.push({ title: item.value, value: item.code });
          });
        // 基准利率
        let baseRate = [];
        response.ASSETS_JZLLZL &&
          response.ASSETS_JZLLZL.length > 0 &&
          response.ASSETS_JZLLZL.forEach((item) => {
            baseRate.push({ title: item.value, value: item.code });
          });
        // 计息方式
        let interestMethodList = [];
        response.PLANINFO_INTERESTMETHOD_DICT &&
          response.PLANINFO_INTERESTMETHOD_DICT.length > 0 &&
          response.PLANINFO_INTERESTMETHOD_DICT.forEach((item) => {
            interestMethodList.push({ title: item.value, value: item.code });
          });
        yield put({
          type: 'save',
          payload: {
            assetTypeList: temAssetType, // 资产类别
            fundActualInvestList: tempFundActualInvest, // 资金实际投向
            planPropertyList: planProperty, // 资产管理计划属性
            managementStyleList: managementStyle, // 管理方式
            buyConstructionList: buyConstruction, // 购买结构
            interestBasisList: interestBasis, //计息基础
            rateTypeList: rateType,
            splitWayList: splitWayList,
            baseRate: baseRate,
            interestMethodList: interestMethodList,
          },
        });
      } else {
        message.error(`获取字典失败，失败原因：`);
      }
    },
    // 获取行业的枚举接口
    *getFundDictionList({ payload, callback }, { call, put, select }) {
      let response = yield select((state) => state.globalM.dataDictionary['GJTJJHYFLI']);
      if (response && response.length > 0) {
        // 资金运用行业
        let tempFundUseIndustry = [];
        tempFundUseIndustry = response.map((item) => {
          return {
            label: item.value,
            list: item?.children.map((x) => {
              return {
                value: x.code,
                title: `${x.code}-${x.value}`,
              };
            }),
          };
        });
        yield put({
          type: 'save',
          payload: {
            fundUseIndustryList: tempFundUseIndustry, //资金运用行业
          },
        });
      } else {
        message.error(`获取字典失败，失败原因：`);
      }
    },
    // // 资金运用行业 下拉数据
    // *fundUseIndustryData({ payload, callback }, { call, put }) {
    //     const res = yield call(api.outsourceInvestMsgMaintain.fundUseIndustryData, payload);
    //     if (res && res.code == 0) {
    //         yield put({
    //             type: 'save',
    //             payload: {
    //                 fundUseIndustryList: res.data.dataSource?.map(item => {
    //                     return {
    //                         ...item,
    //                         list: item?.list.map(x => {
    //                             return {
    //                                 ...x,
    //                                 title: `${x.value}-${x.title}`
    //                             }
    //                         })
    //                     }
    //                 }),// 资金运用行业
    //             }
    //         })

    //     } else {
    //         message.error(`获取资金运用行业失败，失败原因：${res?.msg}`)
    //         console.error(`获取资金运用行业失败，失败原因：${res?.msg}`, '接口名称： getcategory 接口：portfolio-server/entrustedinvestment/getcategory')
    //     }
    // },
    //获取估值时间
    *getTime({ payload, callback }, { call, put }) {
      const res = yield call(api.qualificationManagement.getTime, payload);

      if (res && res.code == 0) {
        callback('success', res.data);
      } else {
        callback('fail');
      }
    },
    // 获取估值解析列表
    *findanalysis({ payload, callback }, { call, put }) {
      const res = yield call(api.qualificationManagement.findanalysis, payload);

      if (res && res.code == 0) {
        callback('success', res);
      } else {
        callback('fail');
      }
    },
    // 获取列表
    *queryTable({ payload, callback }, { call, put, select }) {
      const res = yield call(api.qualificationManagement.queryTable, payload);
      let fundUseIndustryList = yield select((state) => state.qualificationManagementPlanMaintenanceM.fundUseIndustryList);
      if (res && res.code == 0) {
        res.data?.dataSource &&
          res.data?.dataSource.length &&
          res.data?.dataSource.forEach((item) => {
            fundUseIndustryList?.length &&
              fundUseIndustryList.forEach((fundItem) => {
                if (fundItem.list?.length) {
                  let tempResult = fundItem.list.find((i) => i.value == item.zjUseHy);
                  if (tempResult) {
                    item.zjUseHy = tempResult.title;
                  }
                }
              });
          });
        callback('success', res.data);
      } else {
        callback('fail');
        message.error(`${res?.message}`);
      }
    },
    // 获取列表
    *saveData({ payload, callback }, { call, put }) {
      const res = yield call(api.qualificationManagement.saveData, payload);
      if (res && res.success == true) {
        callback('success', res.data);
      } else {
        callback('fail');
        message.error(`${res?.message}`);
      }
    },
    // 获取详情
    *queryOne({ payload, callback }, { call, put }) {
      const res = yield call(api.qualificationManagement.queryOne, payload);
      if (res && res.success == true) {
        callback('success', res.data);
      } else {
        callback('fail');
        message.error(`${res?.message}`);
      }
    },
    // 删除资产管理
    *deleteOne({ payload, callback }, { call, put }) {
      const res = yield call(api.qualificationManagement.deleteOne, payload);
      if (res && res.success == true) {
        message.success('删除成功');
        callback('success', res.data);
      } else {
        callback('fail');
        message.error(`${res?.message}`);
      }
    },
    // 获取详情
    *saveValuateAnalysis({ payload, callback }, { call, put }) {
      const res = yield call(api.qualificationManagement.saveValuateAnalysis, payload);
      if (res && res.code == 0) {
        callback('success');
        message.success('保存成功');
      } else {
        callback('fail');
        message.error(`${res?.message}`);
      }
    },
    // 展期
    *deferDate({ payload, callback }, { call, put }) {
      const res = yield call(api.qualificationManagement.deferDate, payload);
      if (res && res.success) {
        message.success('展期成功!');
        callback('success');
      } else {
        callback('fail');
        message.error(`展期失败，失败原因：${res?.message}`);
        console.error(`展期失败，失败原因：${res?.message}`, '接口名称： saveById 接口：zgtzptPlanInfoPro/saveById');
      }
    },
    // 获取发行人下拉列表
    *getIssuerList({ payload, callback }, { call, put }) {
      const response = yield call(api.getIssuerList, payload);
      if (response && response.code == 0) {
        callback(response.data.dataSource);
      } else {
        message.error(`获取发行人数据失败，失败原因：${response?.msg}`);
        console.error(`获取发行人数据失败，失败原因：${response?.msg}`, '接口名称： issuerInfo 接口：instrument-server/bondissuerinfo/issuerInfo');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
